<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="业务标识" prop="biz_type">
        <el-select v-model="queryParams.biz_type" placeholder="业务标识">
          <el-option
            v-for="dict in dict.type.i18n_biz_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="语言" prop="lang">
        <el-select v-model="queryParams.lang" placeholder="语言">
          <el-option
            v-for="dict in dict.type.i18n_lang"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务分片" prop="biz_section">
        <el-input
          v-model="queryParams.biz_section"
          placeholder="请输入业务分片"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="code" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入参code"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-upload action="#" :http-request="requestUpload" :show-file-list="false"
                   :before-upload="beforeUploadPreviewExcel">
          <el-button
            type="danger"
            plain
            icon="el-icon-upload"
            size="mini"
            v-hasPermi="['system:i18n:import']"
          >导入翻译文件
          </el-button>
        </el-upload>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:i18n:export']"
        >导出翻译文件
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="translate"
          v-hasPermi="['system:i18n:translate']"
        >一键翻译
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="i18nList">
      <el-table-column label="主键" align="center" prop="id"/>
      <el-table-column label="业务标识" align="center" prop="biz_type"/>
      <el-table-column label="业务分片" align="center" prop="biz_section"/>
      <el-table-column label="code" align="center" prop="code"/>
      <el-table-column label="语言资源(中文)" align="center" prop="description"/>
      <el-table-column label="'语言资源(翻译后)'" align="center" prop="i18n_description"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:i18n:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:i18n:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.page_size"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="code" prop="code">
          <textarea v-model="form.description" placeholder="请输入语言资源(翻译)"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  delPlatformI18n,
  importExcelPlatformI18n,
  listPlatformI18n,
  translatePlatformI18n,
  updatePlatformI18n
} from "@/api/system/i18n";

export default {
  name: "I18n",
  dicts: ['i18n_lang', 'i18n_biz_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      i18nList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // excel文件
      preview_excel_file: undefined,
      // 查询参数
      queryParams: {
        page: 1,
        page_size: 10,
        biz_type: 'manager',
        lang: 'en',
        biz_section: undefined,
        code: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        biz_type: [
          {required: true, message: "业务标识不能为空", trigger: "change"}
        ],
        biz_section: [
          {required: true, message: "业务分片不能为空", trigger: "change"}
        ],
        code: [
          {required: true, message: "code不能为空", trigger: "change"}
        ],
        lang: [
          {required: true, message: "语言不能为空", trigger: "change"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listPlatformI18n(this.queryParams).then(response => {
          this.i18nList = response.data;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        biz_type: undefined,
        biz_section: undefined,
        code: undefined,
        lang: undefined,
        description: undefined,
        lang_description_list: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.biz_type = row.biz_type
      this.form.biz_section = row.biz_section
      this.form.code = row.code
      this.form.description = row.i18n_description
      this.form.lang = row.i18n_lang
      this.open = true;
      this.title = "修改";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.lang_description_list = [
            {
              "lang": this.form.lang,
              "description": this.form.description
            }
          ]
          updatePlatformI18n(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除参数编号为"' + ids + '"的数据项？').then(function () {
        return delPlatformI18n(
          {
            biz_type: row.biz_type,
            biz_section: row.biz_section,
            code: row.code,
            lang_list: [row.i18n_lang]
          }
        );
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.downloadExcel('/metadata/v1/i8n/platform/export_excel', this.queryParams)
    },
    // 上传预处理
    beforeUploadPreviewExcel(file) {
      const isXlS = file.type === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        || file.type === "application/vnd.ms-excel";
      if (!isXlS) {
        this.$modal.msgError("文件格式错误，请上传图片类型,如：XLSX后缀的文件。");
      }
    },
    // 覆盖默认的上传行为
    requestUpload(file) {
      const formData = new FormData()
      formData.append('biz_type', this.queryParams.biz_type)
      formData.append('lang', this.queryParams.lang)
      formData.append('file', file.file)
      importExcelPlatformI18n(formData).then(() => {
        this.$modal.msgSuccess("上传成功")
        this.getList();
      });
    },
    // 一键翻译
    translate() {
      translatePlatformI18n({biz_type: this.queryParams.biz_type, lang: this.queryParams.lang}).then(() => {
        this.$modal.msgSuccess("一键翻译成功")
        this.getList();
      });
    }
  }
};
</script>
